<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Verify;
class UserController extends Controller {
    public function login(){
        header('Content-type:text/html;charset=utf-8');
        $user = D('Manager');
        if(!empty($_POST)){
            $verify = new Verify();
            if($verify->check($_POST['captcha'])){
                $userinfo = array(
                    'mg_name'=>$_POST['admin_user'],
                    'mg_pwd'=>$_POST['admin_psd']
                );
                $res = $user -> where($userinfo)->find();
                if($res){
                    session('admin_name',$res['mg_name']);
                    session('admin_id',$res['mg_id']);
                    $this->redirect('Index/index');
                } else {
                    $this->error("用户名或密码错误");
                }
            } else {
                //$error = $user->getError();
                //$this->error("验证码错误");
                //echo $error;
                $this->display();
            }
        } else {
            //$error = $user->getError();
            //$this->assign('error',$error);
            //$this->error("验证码错误");
            $this->display();
        }
    }
    
    public function logout(){
        session(null);
        $this->redirect('User/login');
    }
    
    //生成验证码
    public function verifyImg(){
        $cfg = array(
            'imageH' =>40,
            'imageW' =>100,
            'fontSize' =>15,
            'length' =>4,
            'fontttf' =>'4.ttf',
        );
        //实例化验证码
        
        $verifi = new \Think\Verify($cfg);
        $verifi->entry();
    }
    
    public function register(){
        $this->display();
    }
    
}